



<!DOCTYPE html>
<html lang="zh-CN">
  <head><meta name="generator" content="Hexo 3.9.0">
    <title>从 Vue Router 0.7.x 迁移 — Vue.js</title>
    <meta charset="utf-8">
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="alternate" hreflang="x-default" href="https://vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="zh" href="https://cn.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="ja" href="https://jp.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="ru" href="https://ru.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="ko" href="https://kr.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="pt-BR" href="https://br.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="fr" href="https://fr.vuejs.org/v2/guide/migration-vue-router.html">
    <link rel="alternate" hreflang="es" href="https://es.vuejs.org/v2/guide/migration-vue-router.html">

    <meta property="og:type" content="article">
    <meta property="og:title" content="从 Vue Router 0.7.x 迁移 — Vue.js">
    <meta property="og:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta property="og:image" content="https://cn.vuejs.org/images/logo.png">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="从 Vue Router 0.7.x 迁移 — Vue.js">
    <meta name="twitter:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="twitter:image" content="https://cn.vuejs.org/images/logo.png">

    <link rel="apple-touch-icon" sizes="57x57" href="/vue2/images/icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/vue2/images/icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/vue2/images/icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/vue2/images/icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/vue2/images/icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/vue2/images/icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/vue2/images/icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/vue2/images/icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/vue2/images/icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="/vue2/images/icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/vue2/images/icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="/vue2/images/icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/vue2/images/icons/favicon-16x16.png">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <link rel="icon" href="/vue2/images/logo.svg">

    <meta name="msapplication-TileColor" content="#4fc08d">
    <meta name="theme-color" content="#4fc08d">

    <meta name="msapplication-config" content="browserconfig.xml">
    <link rel="manifest" href="/vue2/manifest.json">

    <!-- <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com" rel="preconnect" crossorigin> -->

    <!-- <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600|Roboto Mono&display=swap" rel="stylesheet"> -->
    <!-- <link href="https://fonts.googleapis.com/css?family=Dosis:500&text=Vue.js&display=swap" rel="stylesheet"> -->

    <link href="//code.bdstatic.com/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

    <!-- main page styles -->
    <link rel="stylesheet" href="/vue2/css/page.css">

    <!-- this needs to be loaded before guide's inline scripts -->
    <script src="/vue2/js/vue.js"></script>

    

    <script>
      Vue.config.productionTip = false
      window.PAGE_TYPE = "guide"
    </script>

    <!-- ga -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-46852172-3', 'cn.vuejs.org');
      ga('send', 'pageview');
    </script>

    <!-- vimeo analytics -->
    <!-- <script type="text/javascript" defer="defer" src="https://extend.vimeocdn.com/ga/72160148.js"></script> -->
</head>
  <body class="docs">

    

    <div id="mobile-bar">
      <a class="menu-button"></a>
      <a class="logo" href="/"></a>
    </div>
    <div>
  <div id="v3-banner">
    <span class="hidden-sm">非官方文档，仅为个人学习使用，查看官方文档请移步</span><a href="https://cn.vuejs.org/" target="_blank">这里</a>。
  </div>

  <header id="header">
    <a id="logo" href="/vue2/">
      <img src="/vue2/images/logo.svg" alt="vue logo">
      <span>Vue.js</span>
    </a>
    <ul id="nav">
      <li>
  <form id="search-form">
    <input type="text" id="search-query-nav" class="search-query st-default-search-input" aria-label="搜索">
  </form>
</li>
<li class="nav-dropdown-container learn">
  <a class="nav-link current">学习</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>文档</h4></li>
    <li>
      <ul>
        <li><a href="/vue2/v2/guide/" class="nav-link current">教程</a></li>
        <li><a href="/vue2/v2/api/" class="nav-link">API</a></li>
        <li><a href="/vue2/v2/style-guide/" class="nav-link">风格指南</a></li>
        <li><a href="/vue2/v2/examples/" class="nav-link">示例</a></li>
        <li><a href="/vue2/v2/cookbook/" class="nav-link">Cookbook</a></li>
      </ul>
    </li>
    <li><h4>视频教程</h4></li>
    <li>
      <ul>
        <li>
          <a href="https://www.vuemastery.com/courses/" class="nav-link" target="_blank" rel="sponsored noopener">
            Vue Mastery (英文)
          </a>
        </li>
        <li>
          <a href="https://vueschool.io/?friend=vuejs&utm_source=Vuejs.org&utm_medium=Link&utm_content=Navbar%20Dropdown" class="nav-link" target="_blank" rel="sponsored noopener">
            Vue School (英文)
          </a>
        </li>
        <!-- start: special logic for cn -->
        <li>
          <a href="https://learning.dcloud.io/" class="nav-link" target="_blank" rel="sponsored noopener">
            DCloud 视频教程
          </a>
        </li>
        <!-- end: special logic for cn -->
      </ul>
    </li>
  </ul>
</li>

<li class="nav-dropdown-container ecosystem">
  <a class="nav-link">生态系统</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>帮助</h4></li>
    <li><ul>
      <li><a href="https://forum.vuejs.org/" class="nav-link" target="_blank" rel="noopener">论坛</a></li>
      <li><a href="https://chat.vuejs.org/" class="nav-link" target="_blank" rel="noopener">聊天室</a></li>
      <li><a href="https://events.vuejs.org/meetups/" class="nav-link" target="_blank" rel="noopener">聚会</a></li>
    </ul></li>
    <li><h4>工具</h4></li>
    <li>
      <ul>
        <li><a href="https://github.com/vuejs/vue-devtools" class="nav-link" target="_blank" rel="noopener">Devtools</a></li>
        <li><a href="https://cli.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue CLI</a></li>
        <li><a href="https://vue-loader.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Loader</a></li>
      </ul>
    </li>
    <li><h4>核心插件</h4></li>
    <li><ul>
      <li><a href="https://router.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Router</a></li>
      <li><a href="https://vuex.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vuex</a></li>
      <li><a href="https://ssr.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue 服务端渲染</a></li>
    </ul></li>
    <li><h4>信息</h4></li>
    <li><ul>
      <li><a href="https://news.vuejs.org" class="nav-link" target="_blank" rel="noopener">周刊</a></li>
      <li><a href="https://github.com/vuejs/vue/projects/6" class="nav-link" target="_blank" rel="noopener">Roadmap</a></li>
      <li><a href="https://events.vuejs.org/" class="nav-link" target="_blank" rel="noopener">活动</a></li>
      <li><a href="https://twitter.com/vuejs" class="nav-link" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://medium.com/the-vue-point" class="nav-link" target="_blank" rel="noopener">博客</a></li>
      <li><a href="https://vuejobs.com/?ref=vuejs" class="nav-link" target="_blank" rel="noopener">工作</a></li>
      <li><a href="https://dev.to/t/vue" class="nav-link" target="_blank" rel="noopener">DEV 社区</a></li>
    </ul></li>
  </ul>
</li>

<li>
  <a href="/vue2/v2/guide/team.html" class="nav-link team">团队</a>
</li>
<li class="nav-dropdown-container resources">
  <a href="#" class="nav-link">资源列表</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="/vue2/resources/partners.html" class="nav-link">合作伙伴</a></li>
    <li><a href="/vue2/resources/themes.html" class="nav-link">主题</a></li>
    <li><a href="https://github.com/vuejs/awesome-vue" class="nav-link" target="_blank" rel="noopener">Awesome Vue</a></li>
    <li><a href="https://awesomejs.dev/for/vue/" class="nav-link" target="_blank" rel="noopener">浏览和 Vue 相关的包</a></li>
  </ul>
</li>

<!-- <li>
  <a href="/vue2/partners" class="nav-link ">Partners</a>
</li> -->
<li class="nav-dropdown-container support-vue">
  <a href="/support-vuejs/" class="nav-link">支持 Vue</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="/support-vuejs/#One-time-Donations" class="nav-link">一次性赞助</a></li>
    <li><a href="/support-vuejs/#Recurring-Pledges" class="nav-link">周期性赞助</a></li>
    <!-- start: special logic for cn -->
    <li><a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" rel="noopener" class="nav-link">贴纸</a></li>
    <li><a href="https://osholic.com/?utm_source=vue&utm_medium=dropdown" rel="noopener" class="nav-link">周边</a></li>
    <!-- end: special logic for cn -->
    <li><a href="https://vue.threadless.com" target="_blank" rel="noopener" class="nav-link">T 恤商店</a></li>
  </ul>
</li>

<li class="nav-dropdown-container language">
  <a class="nav-link">多语言</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">English</a></li>
    <li><a href="https://jp.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">日本語</a></li>
    <li><a href="https://ru.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">Русский</a></li>
    <li><a href="https://kr.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">한국어</a></li>
    <li><a href="https://br.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">Português</a></li>
    <li><a href="https://fr.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">Français</a></li>
    <li><a href="https://vi.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">Tiếng Việt</a></li>
    <li><a href="https://es.vuejs.org/v2/guide/migration-vue-router.html" class="nav-link">Español</a></li>
    <li><a href="https://docs.vuejs.id/v2/guide/migration-vue-router.html" class="nav-link">Bahasa Indonesia</a></li>
  </ul>
</li>

<!-- start: special logic for cn -->
<li><a href="https://github.com/vuejs/cn.vuejs.org/" target="_blank" class="nav-link contribute">参与翻译</a></li>
<!-- end: special logic for cn -->

    </ul>
  </header>
</div>

    
      <div id="main" class="fix-sidebar">
        
  

<div class="sidebar">
  <div class="sidebar-inner">
    
    <div class="list">
      <div id="sidebar-sponsors-special">
  <div class="main-sponsor">
    <!-- start: special logic for cn -->
    <span>特别赞助商</span>
    <div>
    <a href="http://www.dcloud.io/?hmsr=vuejsorg&hmpl=&hmcu=&hmkw=&hmci=" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-dcloud" alt="dcloud">
      <script>
      (function(){
        var el = document.getElementById("s-dcloud");
        var adImg = null;
        var altImg = "/vue2/images/dcloud2.png";
        var img = "/vue2/images/dcloud1.png";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <div>
    <a href="https://authing.cn/welcome?from=vuejs" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-authing" alt="authing">
      <script>
      (function(){
        var el = document.getElementById("s-authing");
        var adImg = null;
        var altImg = null;
        var img = "/vue2/images/authing.svg";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <div>
    <a href="https://qingfuwu.cn?utm_campaign=TR_eSJH7nqW&utm_content=&utm_medium=Web&utm_source=CH_53OxwryD&utm_term=" target="_blank" rel="sponsored noopener" class="logo">
      <img id="s-qingfuwu" alt="qingfuwu">
      <script>
      (function(){
        var el = document.getElementById("s-qingfuwu");
        var adImg = null;
        var altImg = null;
        var img = "/vue2/images/qingfuwu.svg";
        el.src = adImg || (altImg && Math.random() >= 0.5 ? altImg : img);
      })()
      </script>
    </a>
    </div>
    <!-- end: special logic for cn -->
  </div>
</div>
<!--
<div id="sidebar-sponsors-platinum-left">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/retool.png" alt="Retool">
    </a>
    <a href="https://passionatepeople.io/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/passionate_people.png" alt="Passionate People">
    </a>
    <a href="https://www.storyblok.com" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/storyblok.png" alt="Storyblok">
    </a>
    <a href="https://ionicframework.com/vue?utm_source=partner&utm_medium=referral&utm_campaign=vuesponsorship&utm_content=vuedocs" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/ionic.png" alt="Ionic">
    </a>
    <a href="https://nuxtjs.org/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/nuxt.png" alt="Nuxt">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/vue2/support-vuejs">
    成为赞助者
  </a>
</div>

-->

      
        <h2>
          
          教程
          
            <select class="version-select">
              <option value="v3">3.x</option>
              <option value="SELF" selected>2.x</option>
              <option value="v1">1.0</option>
              <option value="012">0.12</option>
              <option value="011">0.11</option>
            </select>
          
        </h2>
        <ul class="menu-root">
  
    
    
      
        <li><h3>基础</h3></li>
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/installation.html" class="sidebar-link">安装</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/index.html" class="sidebar-link">介绍</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/instance.html" class="sidebar-link">Vue 实例</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/syntax.html" class="sidebar-link">模板语法</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/computed.html" class="sidebar-link">计算属性和侦听器</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/class-and-style.html" class="sidebar-link">Class 与 Style 绑定</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/conditional.html" class="sidebar-link">条件渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/list.html" class="sidebar-link">列表渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/events.html" class="sidebar-link">事件处理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/forms.html" class="sidebar-link">表单输入绑定</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components.html" class="sidebar-link">组件基础</a>
    </li>
  
    
    
      
      
        <li><h3>深入了解组件</h3></li>
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-registration.html" class="sidebar-link">组件注册</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-props.html" class="sidebar-link">Prop</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-custom-events.html" class="sidebar-link">自定义事件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-slots.html" class="sidebar-link">插槽</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-dynamic-async.html" class="sidebar-link">动态组件 & 异步组件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/components-edge-cases.html" class="sidebar-link">处理边界情况</a>
    </li>
  
    
    
      
      
      
        <li><h3>过渡 & 动画</h3></li>
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/transitions.html" class="sidebar-link">进入/离开 & 列表过渡</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/transitioning-state.html" class="sidebar-link">状态过渡</a>
    </li>
  
    
    
      
      
      
      
        <li><h3>可复用性 & 组合</h3></li>
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/mixins.html" class="sidebar-link">混入</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/custom-directive.html" class="sidebar-link">自定义指令</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/render-function.html" class="sidebar-link">渲染函数 & JSX</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/plugins.html" class="sidebar-link">插件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/filters.html" class="sidebar-link">过滤器</a>
    </li>
  
    
    
      
      
      
      
      
        <li><h3>工具</h3></li>
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/single-file-components.html" class="sidebar-link">单文件组件</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/testing.html" class="sidebar-link">测试</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/typescript.html" class="sidebar-link">TypeScript 支持</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/deployment.html" class="sidebar-link">生产环境部署</a>
    </li>
  
    
    
      
      
      
      
      
      
        <li><h3>规模化</h3></li>
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/routing.html" class="sidebar-link">路由</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/state-management.html" class="sidebar-link">状态管理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/ssr.html" class="sidebar-link">服务端渲染</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/security.html" class="sidebar-link">安全</a>
    </li>
  
    
    
      
      
      
      
      
      
      
        <li><h3>内在</h3></li>
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/reactivity.html" class="sidebar-link">深入响应式原理</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
        <li><h3>迁移</h3></li>
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration.html" class="sidebar-link">从 Vue 1.x 迁移</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration-vue-router.html" class="sidebar-link current">从 Vue Router 0.7.x 迁移</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/migration-vuex.html" class="sidebar-link">从 Vuex 0.6.x 迁移到 1.0</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
        <li><h3>更多</h3></li>
      
    
    <li>
      <a href="/vue2/v2/guide/comparison.html" class="sidebar-link">对比其他框架</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/join.html" class="sidebar-link">加入 Vue.js 社区</a>
    </li>
  
    
    
      
      
      
      
      
      
      
      
      
    
    <li>
      <a href="/vue2/v2/guide/team.html" class="sidebar-link">认识团队</a>
    </li>
  
</ul>

      
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-right">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/retool.png" alt="Retool">
    </a>
    <a href="https://passionatepeople.io/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/passionate_people.png" alt="Passionate People">
    </a>
    <a href="https://www.storyblok.com" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/storyblok.png" alt="Storyblok">
    </a>
    <a href="https://ionicframework.com/vue?utm_source=partner&utm_medium=referral&utm_campaign=vuesponsorship&utm_content=vuedocs" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/ionic.png" alt="Ionic">
    </a>
    <a href="https://nuxtjs.org/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/vue2/images/nuxt.png" alt="Nuxt">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/vue2/support-vuejs">
    成为赞助者
  </a>
</div>


-->


<div class="content guide with-sidebar migration-vue-router-guide">
  <p class="tip warning v3-warning">
    您在浏览的是 v2.x 及更早版本的文档。v3.x 的文档<a href="https://wangyubina.github.io/vue3/">在这里</a>。
  </p>

  
    
      
<div id="ad">
  <!-- start: special logic for cn -->
  <div class="wwads-cn wwads-vertical" data-id="53" style="max-width: 200px"></div>
  <script type="text/javascript" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
  <!-- end: special logic for cn -->
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CEBDT27Y&placement=vuejsorg"
    id="_carbonads_js"
  ></script> -->
</div>


    
  
  
    <h1>从 Vue Router 0.7.x 迁移</h1>
  

  <!-- start: special logic for cn -->
  <!-- start: special logic for cn -->
<!-- <div class="ad-pagetop">
  <a href="http://www.vueshop.com.cn/index.php?http_referer=vuejs" target="_blank" rel="noopener noreferrer">
    <span class="name">VUEshop</span>
    <span class="description">中国免费商城系统 - 搜豹商城系统 - 免费50小时 Vue 视频教程</span>
    <span class="action">立即查看 &gt;</span>
  </a>
</div> -->
<!-- end: special logic for cn -->

  <!-- end: special logic for cn -->

  
    <blockquote>
<p>只有 Vue Router 2 是与 Vue 2 相互兼容的，所以如果你更新了 Vue，你也需要更新 Vue Router。这也是我们在主文档中将迁移路径的详情添加进来的原因。有关使用 Vue Router 2 的完整教程，请参阅 <a href="https://router.vuejs.org/zh-cn/" target="_blank" rel="noopener">Vue Router 文档</a>。</p>
</blockquote>
<h2 id="Router-初始化"><a href="#Router-初始化" class="headerlink" title="Router 初始化"></a>Router 初始化</h2><h3 id="router-start-替换"><a href="#router-start-替换" class="headerlink" title="router.start 替换"></a><code>router.start</code> <sup>替换</sup></h3><p>不再会有一个特殊的 API 用来初始化包含 Vue Router 的 app，这意味着不再是：</p>
<pre><code class="hljs js">router.start(&#123;
  <span class="hljs-attr">template</span>: <span class="hljs-string">'&lt;router-view&gt;&lt;/router-view&gt;'</span>
&#125;, <span class="hljs-string">'#app'</span>)</code></pre>
<p>你只需要传一个路由 property 给 Vue 实例：</p>
<pre><code class="hljs js"><span class="hljs-keyword">new</span> Vue(&#123;
  <span class="hljs-attr">el</span>: <span class="hljs-string">'#app'</span>,
  <span class="hljs-attr">router</span>: router,
  <span class="hljs-attr">template</span>: <span class="hljs-string">'&lt;router-view&gt;&lt;/router-view&gt;'</span>
&#125;)</code></pre>
<p>或者，如果你使用的是运行时构建 (runtime-only) 方式：</p>
<pre><code class="hljs js"><span class="hljs-keyword">new</span> Vue(&#123;
  <span class="hljs-attr">el</span>: <span class="hljs-string">'#app'</span>,
  <span class="hljs-attr">router</span>: router,
  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> h(<span class="hljs-string">'router-view'</span>)
&#125;)</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>router.start</code> 被调用的示例。</p>
</div>

<h2 id="Route-定义"><a href="#Route-定义" class="headerlink" title="Route 定义"></a>Route 定义</h2><h3 id="router-map-替换"><a href="#router-map-替换" class="headerlink" title="router.map 替换"></a><code>router.map</code> <sup>替换</sup></h3><p>路由现在被定义为一个在 router 实例里的一个 <a href="https://router.vuejs.org/zh-cn/essentials/getting-started.html#javascript" target="_blank" rel="noopener"><code>routes</code> 选项</a>数组。所以这些路由：</p>
<pre><code class="hljs js">router.map(&#123;
  <span class="hljs-string">'/foo'</span>: &#123;
    <span class="hljs-attr">component</span>: Foo
  &#125;,
  <span class="hljs-string">'/bar'</span>: &#123;
    <span class="hljs-attr">component</span>: Bar
  &#125;
&#125;)</code></pre>
<p>会以这种方式定义：</p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> VueRouter(&#123;
  <span class="hljs-attr">routes</span>: [
    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">'/foo'</span>, <span class="hljs-attr">component</span>: Foo &#125;,
    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">'/bar'</span>, <span class="hljs-attr">component</span>: Bar &#125;
  ]
&#125;)</code></pre>
<p>考虑到不同浏览器中遍历对象不能保证会使用相同的 property 顺序，这种数组的语法可以保证更多可预测的路由匹配。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>router.map</code> 被调用的示例。</p>
</div>

<h3 id="router-on-移除"><a href="#router-on-移除" class="headerlink" title="router.on 移除"></a><code>router.on</code> <sup>移除</sup></h3><p>如果你需要在启动的 app 时通过代码生成路由，你可以动态地向路由数组推送定义来完成这个操作。举个例子：</p>
<pre><code class="hljs js"><span class="hljs-comment">// 普通的路由</span>
<span class="hljs-keyword">var</span> routes = [
  <span class="hljs-comment">// ...</span>
]

<span class="hljs-comment">// 动态生成的路由</span>
marketingPages.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">page</span>) </span>&#123;
  routes.push(&#123;
    <span class="hljs-attr">path</span>: <span class="hljs-string">'/marketing/'</span> + page.slug
    <span class="hljs-attr">component</span>: &#123;
      <span class="hljs-attr">extends</span>: MarketingComponent
      <span class="hljs-attr">data</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
        <span class="hljs-keyword">return</span> &#123; <span class="hljs-attr">page</span>: page &#125;
      &#125;
    &#125;
  &#125;)
&#125;)

<span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> Router(&#123;
  <span class="hljs-attr">routes</span>: routes
&#125;)</code></pre>
<p>如果你需要在 router 被实例化后增加新的路由，你可以把 router 原来的匹配方式换成一个包括你新添的加路由的匹配方式：</p>
<pre><code class="hljs js">router.match = createMatcher(
  [&#123;
    <span class="hljs-attr">path</span>: <span class="hljs-string">'/my/new/path'</span>,
    <span class="hljs-attr">component</span>: MyComponent
  &#125;].concat(router.options.routes)
)</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>router.on</code> 被调用的示例。</p>
</div>

<h3 id="router-beforeEach-changed"><a href="#router-beforeEach-changed" class="headerlink" title="router.beforeEach changed"></a><code>router.beforeEach</code> <sup>changed</sup></h3><p><code>router.beforeEach</code> 现在是异步工作的，并且携带一个 <code>next</code> 函数作为其第三个参数。</p>
<pre><code class="hljs js">router.beforeEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transition</span>) </span>&#123;
  <span class="hljs-keyword">if</span> (transition.to.path === <span class="hljs-string">'/forbidden'</span>) &#123;
    transition.abort()
  &#125; <span class="hljs-keyword">else</span> &#123;
    transition.next()
  &#125;
&#125;)</code></pre>
<pre><code class="hljs js">router.beforeEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">to, from, next</span>) </span>&#123;
  <span class="hljs-keyword">if</span> (to.path === <span class="hljs-string">'/forbidden'</span>) &#123;
    next(<span class="hljs-literal">false</span>)
  &#125; <span class="hljs-keyword">else</span> &#123;
    next()
  &#125;
&#125;)</code></pre>
<h3 id="subRoutes-换名"><a href="#subRoutes-换名" class="headerlink" title="subRoutes 换名"></a><code>subRoutes</code> <sup>换名</sup></h3><p>出于 Vue Router 和其他路由库一致性的考虑，重命名为 <a href="https://router.vuejs.org/zh-cn/essentials/nested-routes.html" target="_blank" rel="noopener"><code>children</code></a></p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>subRoutes</code> 选项的示例。</p>
</div>

<h3 id="router-redirect-替换"><a href="#router-redirect-替换" class="headerlink" title="router.redirect 替换"></a><code>router.redirect</code> <sup>替换</sup></h3><p>现在用一个<a href="https://router.vuejs.org/zh-cn/essentials/redirect-and-alias.html" target="_blank" rel="noopener">路由定义的选项</a>作为代替。举个例子，你将会更新：</p>
<pre><code class="hljs js">router.redirect(&#123;
  <span class="hljs-string">'/tos'</span>: <span class="hljs-string">'/terms-of-service'</span>
&#125;)</code></pre>
<p>成像下面的 <code>routes</code> 配置里定义的样子：</p>
<pre><code class="hljs js">&#123;
  <span class="hljs-attr">path</span>: <span class="hljs-string">'/tos'</span>,
  <span class="hljs-attr">redirect</span>: <span class="hljs-string">'/terms-of-service'</span>
&#125;</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>router.redirect</code> 被调用的示例。</p>
</div>

<h3 id="router-alias-替换"><a href="#router-alias-替换" class="headerlink" title="router.alias 替换"></a><code>router.alias</code> <sup>替换</sup></h3><p>现在是你进行 alias 操作的<a href="https://router.vuejs.org/zh-cn/essentials/redirect-and-alias.html" target="_blank" rel="noopener">路由定义里的一个选项</a>。举个例子，你需要在你的 <code>routes</code> 定义里将：</p>
<pre><code class="hljs js">router.alias(&#123;
  <span class="hljs-string">'/manage'</span>: <span class="hljs-string">'/admin'</span>
&#125;)</code></pre>
<p>配置这个样子：</p>
<pre><code class="hljs js">&#123;
  <span class="hljs-attr">path</span>: <span class="hljs-string">'/admin'</span>,
  <span class="hljs-attr">component</span>: AdminPanel,
  <span class="hljs-attr">alias</span>: <span class="hljs-string">'/manage'</span>
&#125;</code></pre>
<p>如果你需要进行多次 alias 操作，你也可以使用一个数组语法去实现：</p>
<pre><code class="hljs js">alias: [<span class="hljs-string">'/manage'</span>, <span class="hljs-string">'/administer'</span>, <span class="hljs-string">'/administrate'</span>]</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行<a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a>找到 <code>router.alias</code> 被调用的示例。</p>
</div>

<h3 id="任意的-Route-property-替换"><a href="#任意的-Route-property-替换" class="headerlink" title="任意的 Route property 替换"></a>任意的 Route property <sup>替换</sup></h3><p>现在任意的 route property 必须在新 meta property 的作用域内，以避免和以后的新特性发生冲突。举个例子，如果你以前这样定义：</p>
<pre><code class="hljs js"><span class="hljs-string">'/admin'</span>: &#123;
  <span class="hljs-attr">component</span>: AdminPanel,
  <span class="hljs-attr">requiresAuth</span>: <span class="hljs-literal">true</span>
&#125;</code></pre>
<p>你现在需要把它更新成：</p>
<pre><code class="hljs js">&#123;
  <span class="hljs-attr">path</span>: <span class="hljs-string">'/admin'</span>,
  <span class="hljs-attr">component</span>: AdminPanel,
  <span class="hljs-attr">meta</span>: &#123;
    <span class="hljs-attr">requiresAuth</span>: <span class="hljs-literal">true</span>
  &#125;
&#125;</code></pre>
<p>如果在一个路由上访问一个 property，你仍然会通过 meta。举个例子：</p>
<pre><code class="hljs js"><span class="hljs-keyword">if</span> (route.meta.requiresAuth) &#123;
  <span class="hljs-comment">// ...</span>
&#125;</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到任意的路由不在 meta 作用域下的示例。</p>
</div>

<h3 id="URL-中的-Query-数组-语法移除"><a href="#URL-中的-Query-数组-语法移除" class="headerlink" title="URL 中的 Query 数组 [] 语法移除"></a>URL 中的 Query 数组 [] 语法<sup>移除</sup></h3><p>当传递数组给 query 参数时，URL 语法不再是 <code>/foo?users[]=Tom&amp;users[]=Jerry</code>，取而代之，新语法是 <code>/foo?users=Tom&amp;users=Jerry</code>，此时 <code>$route.query.users</code> 将仍旧是一个数组，不过如果在该 query 中只有一个参数：<code>/foo?users=Tom</code>，当直接访问该路由时，vue-router 将无法知道我们期待的 <code>users</code> 是个数组。因此，可以考虑添加一个计算属性并且在每个使用 <code>$route.query.users</code> 的地方以该计算属性代替：</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
  <span class="hljs-comment">// ...</span>
  <span class="hljs-attr">computed</span>: &#123;
    <span class="hljs-comment">// 此计算属性将始终是个数组</span>
    users () &#123;
      <span class="hljs-keyword">const</span> users = <span class="hljs-keyword">this</span>.$route.query.users
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>.isArray(users) ? users : [users]
    &#125;
  &#125;
&#125;</code></pre>
<h2 id="Route-匹配"><a href="#Route-匹配" class="headerlink" title="Route 匹配"></a>Route 匹配</h2><p>路由匹配现在使用 <a href="https://github.com/pillarjs/path-to-regexp" target="_blank" rel="noopener">path-to-regexp</a> 这个包，这将会使得工作与之前相比更加灵活。</p>
<h3 id="一个或者更多的命名参数改变"><a href="#一个或者更多的命名参数改变" class="headerlink" title="一个或者更多的命名参数改变"></a>一个或者更多的命名参数<sup>改变</sup></h3><p>语法稍微有些许改变，所以以 <code>/category/*tags</code> 为例，应该被更新为 <code>/category/:tags+</code>。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到弃用路由语法的示例。</p>
</div>

<h2 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h2><h3 id="v-link-替换"><a href="#v-link-替换" class="headerlink" title="v-link 替换"></a><code>v-link</code> <sup>替换</sup></h3><p><code>v-link</code> 指令已经被一个新的 <a href="https://router.vuejs.org/zh-cn/api/router-link.html" target="_blank" rel="noopener"><code>&lt;router-link&gt;</code> 组件</a>指令替代，这一部分的工作已经被 Vue 2 中的组件完成。这将意味着在任何情况下，如果你拥有这样一个链接：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">v-link</span>=<span class="hljs-string">"'/about'"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></code></pre>
<p>你需要把它更新成：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span></code></pre>
<p>注意：<code>&lt;router-link&gt;</code> 不支持 <code>target=&quot;_blank&quot;</code>，如果你想打开一个新标签页，你必须用 <code>&lt;a&gt;</code> 标签。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>v-link</code> 指令的示例。</p>
</div>

<h3 id="v-link-active-替换"><a href="#v-link-active-替换" class="headerlink" title="v-link-active 替换"></a><code>v-link-active</code> <sup>替换</sup></h3><p><code>v-link-active</code> 也因为指定了一个在 <a href="https://router.vuejs.org/zh-cn/api/router-link.html" target="_blank" rel="noopener"><code>&lt;router-link&gt;</code> 组件</a>上的 tag attribute 而被弃用了。举个例子，你需要更新：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-link-active</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">v-link</span>=<span class="hljs-string">"'/about'"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></code></pre>
<p>成这个写法：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">tag</span>=<span class="hljs-string">"li"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span></code></pre>
<p> <code>&lt;a&gt;</code> 标签将会成为真实的链接 (并且可以获取到正确的跳转)，但是激活的类将会被应用在外部的 <code>&lt;li&gt;</code> 标签上。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>v-link-active</code> 指令的示例</p>
</div>

<h2 id="编程导航"><a href="#编程导航" class="headerlink" title="编程导航"></a>编程导航</h2><h3 id="router-go-改变"><a href="#router-go-改变" class="headerlink" title="router.go 改变"></a><code>router.go</code> <sup>改变</sup></h3><p>为了与 <a href="https://developer.mozilla.org/en-US/docs/Web/API/History_API" target="_blank" rel="noopener">HTML5 History API</a> 保持一致性，<code>router.go</code> 已经被用来作为<a href="https://router.vuejs.org/zh-cn/essentials/navigation.html#routergon" target="_blank" rel="noopener">后退/前进导航</a>，<a href="https://router.vuejs.org/zh-cn/essentials/navigation.html#routerpushlocation" target="_blank" rel="noopener"><code>router.push</code></a> 用来导向特殊页面。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> ，找到 <code>router.go</code> 和 <code>router.push</code> 指令被调用的示例。</p>
</div>

<h2 id="路由选择：Modes"><a href="#路由选择：Modes" class="headerlink" title="路由选择：Modes"></a>路由选择：Modes</h2><h3 id="hashbang-false-移除"><a href="#hashbang-false-移除" class="headerlink" title="hashbang: false 移除"></a><code>hashbang: false</code> <sup>移除</sup></h3><p>Hashbangs 将不再为谷歌需要去爬去一个网址，所以他们将不再成为哈希策略的默认选项。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>hashbang: false</code> 选项的示。</p>
</div>

<h3 id="history-true-替换"><a href="#history-true-替换" class="headerlink" title="history: true 替换"></a><code>history: true</code> <sup>替换</sup></h3><p>所有路由模型选项将被简化成一个单个的 <a href="https://router.vuejs.org/zh-cn/api/options.html#mode" target="_blank" rel="noopener"><code>mode</code> 选项</a>。你需要更新：</p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> VueRouter(&#123;
  <span class="hljs-attr">history</span>: <span class="hljs-string">'true'</span>
&#125;)</code></pre>
<p>成这个写法：</p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> VueRouter(&#123;
  <span class="hljs-attr">mode</span>: <span class="hljs-string">'history'</span>
&#125;)</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>history: true</code> 选项的示。</p>
</div>

<h3 id="abstract-true-替换"><a href="#abstract-true-替换" class="headerlink" title="abstract: true 替换"></a><code>abstract: true</code> <sup>替换</sup></h3><p>所有路由模型选项将被简化成一个单个的 <a href="https://router.vuejs.org/zh-cn/api/options.html#mode" target="_blank" rel="noopener"><code>mode</code> 选项</a>。你需要更新：</p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> VueRouter(&#123;
  <span class="hljs-attr">abstract</span>: <span class="hljs-string">'true'</span>
&#125;)</code></pre>
<p>成这个写法：</p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> VueRouter(&#123;
  <span class="hljs-attr">mode</span>: <span class="hljs-string">'abstract'</span>
&#125;)</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>abstract: true</code> 选项的示例。</p>
</div>

<h2 id="路由选项：Misc"><a href="#路由选项：Misc" class="headerlink" title="路由选项：Misc"></a>路由选项：Misc</h2><h3 id="saveScrollPosition-替换"><a href="#saveScrollPosition-替换" class="headerlink" title="saveScrollPosition 替换"></a><code>saveScrollPosition</code> <sup>替换</sup></h3><p>它已经被替换为可以接受一个函数的 <a href="https://router.vuejs.org/zh-cn/advanced/scroll-behavior.html" target="_blank" rel="noopener"><code>scrollBehavior</code> 选项</a>，所以滑动行为可以完全的被定制化处理 - 甚至为每次路由进行定制也可以满足。这将会开启很多新的可能，但是简单的复制旧的行为：</p>
<pre><code class="hljs js">saveScrollPosition: <span class="hljs-literal">true</span></code></pre>
<p>你可以替换为：</p>
<pre><code class="hljs js">scrollBehavior: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">to, from, savedPosition</span>) </span>&#123;
  <span class="hljs-keyword">return</span> savedPosition || &#123; <span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">0</span> &#125;
&#125;</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>saveScrollPosition: true</code> 选项的示例。</p>
</div>

<h3 id="root-换名"><a href="#root-换名" class="headerlink" title="root 换名"></a><code>root</code> <sup>换名</sup></h3><p>为了与 <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base" target="_blank" rel="noopener">HTML 的 <code>&lt;base&gt;</code> 标签</a>保持一致性，重命名为 <code>base</code>。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>root</code> 选项的示例。</p>
</div>

<h3 id="transitionOnLoad-移除"><a href="#transitionOnLoad-移除" class="headerlink" title="transitionOnLoad 移除"></a><code>transitionOnLoad</code> <sup>移除</sup></h3><p>由于 Vue 的过渡系统 <a href="transitions.html#初始渲染的过渡"><code>appear</code> transition control</a> 的存在，这个选项将不再需要。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>transitionOnLoad: true</code> 选项的示例。</p>
</div>

<h3 id="suppressTransitionError-移除"><a href="#suppressTransitionError-移除" class="headerlink" title="suppressTransitionError 移除"></a><code>suppressTransitionError</code> <sup>移除</sup></h3><p>出于简化钩子的考虑被移除。如果你真的需要抑制过渡错误，你可以使用 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch" target="_blank" rel="noopener"><code>try</code>…<code>catch</code></a> 作为替代。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移指令</a> 找到 <code>suppressTransitionError: true</code> 选项的示例。</p>
</div>

<h2 id="路由挂钩"><a href="#路由挂钩" class="headerlink" title="路由挂钩"></a>路由挂钩</h2><h3 id="activate-替换"><a href="#activate-替换" class="headerlink" title="activate 替换"></a><code>activate</code> <sup>替换</sup></h3><p>使用 <a href="https://router.vuejs.org/zh-cn/advanced/navigation-guards.html#组件内的钩子" target="_blank" rel="noopener"><code>beforeRouteEnter</code></a> 这一组件进行替代。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>activate</code> 钩子的示例。</p>
</div>

<h3 id="canActivate-替换"><a href="#canActivate-替换" class="headerlink" title="canActivate 替换"></a><code>canActivate</code> <sup>替换</sup></h3><p>使用 <a href="https://router.vuejs.org/en/advanced/navigation-guards.html#perroute-guard" target="_blank" rel="noopener"><code>beforeEnter</code></a> 在路由中作为替代。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>canActivate</code> 钩子的示例。</p>
</div>

<h3 id="deactivate-移除"><a href="#deactivate-移除" class="headerlink" title="deactivate 移除"></a><code>deactivate</code> <sup>移除</sup></h3><p>使用 <a href="../api/#beforeDestroy"><code>beforeDestroy</code></a> 或者 <a href="../api/#destroyed"><code>destroyed</code></a> 钩子作为替代。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>deactivate</code> 钩子的示例。</p>
</div>

<h3 id="canDeactivate-替换"><a href="#canDeactivate-替换" class="headerlink" title="canDeactivate 替换"></a><code>canDeactivate</code> <sup>替换</sup></h3><p>在组件中使用 <a href="https://router.vuejs.org/zh-cn/advanced/navigation-guards.html#组件内的钩子" target="_blank" rel="noopener"><code>beforeRouteLeave</code></a> 作为替代。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移路径</a> 找到 <code>canDeactivate</code> 钩子的示例。</p>
</div>

<h3 id="canReuse-false-移除"><a href="#canReuse-false-移除" class="headerlink" title="canReuse: false 移除"></a><code>canReuse: false</code> <sup>移除</sup></h3><p>在新的 Vue 路由中将不再被使用。</p>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>canReuse: false</code> 选项的示例。</p>
</div>

<h3 id="data-替换"><a href="#data-替换" class="headerlink" title="data 替换"></a><code>data</code> <sup>替换</sup></h3><p><code>$route</code> property 是响应式的，所以你可以就使用一个 watcher 去响应路由的改变，就像这样：</p>
<pre><code class="hljs js">watch: &#123;
  <span class="hljs-string">'$route'</span>: <span class="hljs-string">'fetchData'</span>
&#125;,
<span class="hljs-attr">methods</span>: &#123;
  <span class="hljs-attr">fetchData</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-comment">// ...</span>
  &#125;
&#125;</code></pre>

<div class="upgrade-path">
  <h4>升级路径</h4>
  <p>运行 <a href="https://github.com/vuejs/vue-migration-helper" target="_blank" rel="noopener">迁移助手</a> 找到 <code>data</code> 钩子的示例。</p>
</div>

<h3 id="loadingRouteData-移除"><a href="#loadingRouteData-移除" class="headerlink" title="$loadingRouteData 移除"></a><code>$loadingRouteData</code> <sup>移除</sup></h3><p>定义你自己的 property (例如：<code>isLoading</code>)，然后在路由上的 watcher 中更新加载状态。举个例子，如果使用 <a href="https://github.com/mzabriskie/axios" target="_blank" rel="noopener">axios</a> 获取数据：</p>
<pre><code class="hljs js">data: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
  <span class="hljs-keyword">return</span> &#123;
    <span class="hljs-attr">posts</span>: [],
    <span class="hljs-attr">isLoading</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">fetchError</span>: <span class="hljs-literal">null</span>
  &#125;
&#125;,
<span class="hljs-attr">watch</span>: &#123;
  <span class="hljs-string">'$route'</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>
    self.isLoading = <span class="hljs-literal">true</span>
    self.fetchData().then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
      self.isLoading = <span class="hljs-literal">false</span>
    &#125;)
  &#125;
&#125;,
<span class="hljs-attr">methods</span>: &#123;
  <span class="hljs-attr">fetchData</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>
    <span class="hljs-keyword">return</span> axios.get(<span class="hljs-string">'/api/posts'</span>)
      .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>&#123;
        self.posts = response.data.posts
      &#125;)
      .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>&#123;
        self.fetchError = error
      &#125;)
  &#125;
&#125;</code></pre>

  
  
    <div class="guide-links">
      
      
        <span>← <a href="/vue2/v2/guide/migration.html">从 Vue 1.x 迁移</a></span>
      
      
      
        <span style="float: right;"><a href="/vue2/v2/guide/migration-vuex.html">从 Vuex 0.6.x 迁移到 1.0</a> →</span>
      
    </div>
  
  <div class="footer">
      <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div id="bsa-native"></div>
<script>
_bsa.init('custom', 'CKYD62QM', 'placement:vuejsorg',
  {
    target: '#bsa-native',
    template: '<a class="native-box" href="##statlink##"><div class="native-sponsor">Sponsor</div><div class="native-text"><strong>##company##</strong> — ##description##</div></a>'
  }
);
</script>


    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/guide/migration-vue-router.md" rel="noopener" target="_blank">
      在 GitHub 上编辑此页！
    </a>
    <!-- Deployed on
    <a href="https://url.netlify.com/HJ8X2mxP8" rel="noopener" target="_blank">
      Netlify
    </a>. -->
  </div>
</div>

      </div>
      <script src="/vue2/js/smooth-scroll.min.js"></script>
    

    <!-- main custom script for sidebars, version selects etc. -->
    <script src="/vue2/js/css.escape.js"></script>
    <script src="/vue2/js/common.js"></script>

    <!-- search -->
    <link href="//code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="/vue2/css/search.css">
    <script src="//code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.js"></script>
    <script>
    [
      '#search-query-nav',
      '#search-query-sidebar',
      '#search-query-menu'
    ].forEach(function (selector) {
      if (!document.querySelector(selector)) return
      // search index defaults to v2
      var match = window.location.pathname.match(/^\/(v\d+)/)
      var version = match ? match[1] : 'v2'
      docsearch({
      appId: 'BH4D9OD16A',
      apiKey: '5638280abff9d207417bb03be05f0b25',
      indexName: 'vuejs_cn2',
      inputSelector: selector,
      algoliaOptions: { facetFilters: ["version:" + version] },
      autocompleteOptions: { hint: false, appendTo: 'body'}
      })
    })
    </script>
  </body>
</html>
